package cn.tedu.cateringsystem.controller;

import cn.tedu.cateringsystem.mapper.DishMapper;
import cn.tedu.cateringsystem.pojo.dto.DishDTO;
import cn.tedu.cateringsystem.pojo.entity.Dish;
import cn.tedu.cateringsystem.pojo.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/dish/")
public class DishController {
    @Autowired
    DishMapper mapper;
    @RequestMapping("insert")
    public void insert(@RequestBody DishDTO dishDTO){
        System.out.println("dishDTO = " + dishDTO);
        Dish dish=new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dish.setSaleCount(0);
        mapper.insert(dish);
    }
    @RequestMapping("select")
    public List<DishVO> select(){
        return mapper.select();
    }
    @Value("$imagesPath")
    private String dirPath;
    @RequestMapping("delete")
    public void deleteById(int id){
        String url = mapper.selectUrlById(id);
        new File(dirPath+url).delete();
        mapper.deleteById(id);
    }
    @RequestMapping("selectByTypeId")
    public List<DishVO> selectByTypeId(int id){
        return mapper.selectByTypeId(id);
    }
}
